#define _LPTIM_C_
/* Includes ---------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lptim.h"
/* USER CODE END Includes */

/* Types ------------------------------------------------------------*/
/* USER CODE BEGIN Types */

/* USER CODE END Types */

/* Constants --------------------------------------------------------*/
/* USER CODE BEGIN Constants */

/* USER CODE END Constants */

/* Macro ------------------------------------------------------------*/
/* USER CODE BEGIN Macro */

/* USER CODE END Macro */

/* Variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
uint16_t uiRGBFlashingTimer = 0;
/* USER CODE END Variables */

/* Function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FP */

/* USER CODE END FP */

/**
  * @brief  LPTIM Initialization function
  * @param  void
  * @retval None
  */
void LPTIM32TimerBaseInit(void)
{
	FL_NVIC_ConfigTypeDef InterruptConfigStruct;
	FL_LPTIM32_InitTypeDef TimerBase_InitStruct;

    TimerBase_InitStruct.clockSource = FL_CMU_LPTIM32_CLK_SOURCE_APBCLK;
    TimerBase_InitStruct.prescalerClockSource = FL_LPTIM32_CLK_SOURCE_INTERNAL;
    TimerBase_InitStruct.prescaler = FL_LPTIM32_PSC_DIV8;
    TimerBase_InitStruct.autoReload = 20000; // 20ms
    TimerBase_InitStruct.mode = FL_LPTIM32_OPERATION_MODE_NORMAL;
    TimerBase_InitStruct.onePulseMode = FL_LPTIM32_ONE_PULSE_MODE_CONTINUOUS;
    TimerBase_InitStruct.triggerEdge = FL_LPTIM32_ETR_TRIGGER_EDGE_RISING;
    TimerBase_InitStruct.countEdge = FL_LPTIM32_ETR_COUNT_EDGE_RISING;

    FL_LPTIM32_Init(LPTIM32, &TimerBase_InitStruct);
	
	FL_LPTIM32_ClearFlag_Update(LPTIM32);
    FL_LPTIM32_EnableIT_Update(LPTIM32);

    InterruptConfigStruct.preemptPriority = 0x03;
    FL_NVIC_Init(&InterruptConfigStruct, LPTIMx_IRQn);
}

/**
  * @brief  Enable LPTIM function
  * @param  void
  * @retval None
  */
void LPTIM32Setup(void)
{
    FL_LPTIM32_Enable(LPTIM32);
}

/**
  * @brief  LPTIM Interrupt function
  * @param  void
  * @retval None
  */
void LPTIM_IRQHandler(void)
{
    if(FL_LPTIM32_IsEnabledIT_Update(LPTIM32) && FL_LPTIM32_IsActiveFlag_Update(LPTIM32))
    {
        FL_LPTIM32_ClearFlag_Update(LPTIM32);
		
		//FL_GPIO_ToggleOutputPin(GPIOA, FL_GPIO_PIN_10);
			
		if(GET_USART_RX_START_FLG())
		{
			gucUsartRxTimerCnt++;
			if(gucUsartRxTimerCnt >= 2) // 20ms*2=40ms
			{
				gucUsartRxTimerCnt = 0;
				SET_USART_RX_FINISHED_FLG();			
				CLR_USART_RX_START_FLG();
			}
		}
		
		if(GET_R_Flashing() || GET_G_Flashing() || GET_B_Flashing())
		{
			uiRGBFlashingTimer++;
			
			if(uiRGBFlashingTimer >= 50) // 20ms*50=1000ms
			{
				uiRGBFlashingTimer = 0;
				
				if(GET_R_Flashing())
				{
					FL_GPIO_ToggleOutputPin(GPIOC, FL_GPIO_PIN_8);
				}
				else if(GET_G_Flashing())
				{
					FL_GPIO_ToggleOutputPin(GPIOC, FL_GPIO_PIN_7);
				}
				else if(GET_B_Flashing())
				{
					FL_GPIO_ToggleOutputPin(GPIOC, FL_GPIO_PIN_6);
				}
			}
		}
    }
}
